/**
 * @file logger
 */
 var debug = require('debug')('morgan')

 var clfmonth = [
   'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
   'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
 ];

/**
 * logger
 *
 * @param  {string|Function} type 日志类型
 * @param  {Object} options 请求类型
 */
var logger = function (type, options) {
    var type = type;
    var opts = options || {};

    if (type && typeof type === 'object') {
        opts = type;
        type = opts.type || 'default';
    }

    if (type === undefined) {
        throw new Error('logger type can not be undefined');
    }

    var stream = opts.stream || process.stdout;

    return function logger(req, res, next) {
        req._startAt = req._startTime = undefined;
        res._startAt = res._startTime = undefined;

        recordStartTime.call(req);

        function logRequest() {
            var formatLine = typeof type === 'function' ? type : getLoggerType(type);
            var line = formatLine(req, res);

            if (null == line) {
                debug('skip line')
                return
            }

            debug('log request')

            stream.write(line + '\n')
        }

        logRequest();

        next();
    };
};


function recordStartTime() {
    this._startAt = process.hrtime()
    this._startTime = new Date()
}


function getLoggerType(type) {
    var fn = type || 'default';
    return typeof fn === 'function' ? fn : require('./logger/' + fn);
}

/**
 * format logger template
 *
 * @param  {string} format
 * @return {Function}
 */
function compile(format) {
    if (typeof format !== 'string') {
        throw new Error('format in compile must be a string');
    }

}
module.exports = logger;
